package com.woyou.ui.component;

import java.util.ArrayList;
import java.util.List;

import com.citaq.ideliver.R;
import com.woyou.bean.SortType;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

/**
 * 商铺类型筛选下拉POP
 * 
 * @author longtao.li
 * 
 */
public class SortTypePop implements OnClickListener {

	private static Context mContext;
	private static View mAnchor;
	private static View mShade; ////背景的遮罩
	private PopupWindow mPopupWindow;
	private View popView;
	private static SortTypePop instance;

	private List<SortType> types = new ArrayList<SortType>();
	private SortType sortTemp;
	private SortTypeListener mListener;

	private SortTypePop() {
	}

	public void init() {
		List<SortType> list = new ArrayList<SortType>();
		if ( types!=null ){
			types.clear();
		}
		
		SortType type = new SortType();
		type.code = "1";
		type.name = "距离最近";
		list.add(type);

		SortType type1 = new SortType();
		type1.code = "2";
		type1.name = "起送价最低";
		list.add(type1);

		SortType type2 = new SortType();
		type2.code = "3";
		type2.name = "销量最高";
		list.add(type2);

		types.addAll(list);
		
		sortTemp = type;
		TextView text = (TextView) mAnchor.findViewById(R.id.shops_filter_conditions_tv);
		text.setText(sortTemp.name);
	}

	public static SortTypePop getInstance(Context context, View anchor, View shade) {
		mContext = context;
		mAnchor = anchor;
		mShade = shade;
		if (instance == null) {
			instance = new SortTypePop();
		}
		return instance;
	}

	public void showPop() {
		if (types.size() == 0 || types == null)
			return;
		if (mPopupWindow == null) {
			popView = View.inflate(mContext, R.layout.pop_sort_bar,null);
			int h = LayoutParams.WRAP_CONTENT;
			if (types.size() > 10) {
				h = (int) mContext.getResources().getDimension(R.dimen.dp1100);
			}
			mPopupWindow = new PopupWindow(popView,LayoutParams.WRAP_CONTENT, h);
			mPopupWindow.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss() {
					mShade.setVisibility(View.GONE);
				}
			});
			if (sortTemp == null) {
				sortTemp = types.get(0);
			}
		}
		mShade.setVisibility(View.VISIBLE);
		refreshList();

		mPopupWindow.setBackgroundDrawable(new ColorDrawable(00000000));// 背景透明
		mPopupWindow.setFocusable(true);
		int w = (int)mContext.getResources().getDimension(R.dimen.dp80);
		int h = (int) mContext.getResources().getDimension(R.dimen.dp0);
		mPopupWindow.showAsDropDown(mAnchor, -w, h);
	}

	private void refreshList() {
		LinearLayout list = (LinearLayout) popView.findViewById(R.id.list);
		list.removeAllViews();

		List<SortType> tempList = new ArrayList<SortType>();
		tempList.addAll(types);
		tempList.remove(sortTemp);

		for (SortType s : tempList) {
			View child = View.inflate(mContext, R.layout.item_sort_pop, null);
			TextView tv = (TextView) child.findViewById(R.id.text);
			child.setTag(s);
			child.setOnClickListener(this);
			child.setBackgroundResource(R.drawable.shop_pop_click_bg);
			tv.setText(s.name);
			list.addView(child);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.sort_item:
			SortType s = (SortType) v.getTag();
			TextView text = (TextView) mAnchor.findViewById(R.id.shops_filter_conditions_tv);
			text.setText(s.name);
			sortTemp = s;
			mPopupWindow.dismiss();
			refreshList();
			if (mListener != null) {
				mListener.itemClick(s);
			}
			break;

		default:
			break;
		}

	}
	
	/**
	 * 隐藏pop
	 */
	public void dismissPop(){
		if ( mPopupWindow!=null && mPopupWindow.isShowing() ){
			mPopupWindow.dismiss();
		}
	}

	public interface SortTypeListener {
		void itemClick(SortType sortType);
	}

	public void setItemClickListener(SortTypeListener l) {
		this.mListener = l;
	}

}
